Skip to content

Modify loopback.rest to include loopback.token - #263

Merged
bajtos merged 1 commit into
masterfrom
feature/include-token-in-rest-middleware-alt
May 21, 2014
Merged

bajtos merged 1 commit into
masterfrom
feature/include-token-in-rest-middleware-alt

Conversation

@bajtos

@bajtos bajtos commented May 16, 2014

Copy link
Copy Markdown
Member

Make loopback.rest self-contained, so that authentication works out of the box.

var app = loopback();
app.enableAuth();
app.use(loopback.rest());

Note that cookie parsing middleware is not added, users have to explicitly configure that if they want to store access tokens in cookies.

Modify loopback.token to skip token lookup when the request already contains accessToken property. This is in line with other connect-based middleware like cookieParser or json and allows developers to install their custom instance of loopback.token middleware before loopback.rest:

var app = loopback();
// setup models & datasources
app.enableAuth();

app.use(loopback.token({ model: MyToken }));
app.use(loopback.rest());
app.listen();

This patch is based on the older pull request #167. The major difference is how the AccessToken model is resolved - #167 used a new method app.getModelByType, this patch uses existing loopback.getModelByType.

/to @raymondfeng @ritch please review

@slnode

slnode commented May 16, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/1209/

@bajtos

bajtos commented May 16, 2014

Copy link
Copy Markdown
Member Author

come on, @slnode! test please

@slnode

slnode commented May 16, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/1210/

@slnode

slnode commented May 16, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/1216/

@bajtos

bajtos commented May 16, 2014

Copy link
Copy Markdown
Member Author

Ignore ci.strongloop.com, here is the Travis build that passed: https://travis-ci.org/strongloop/loopback/builds/25337075

@raymondfeng

Copy link
Copy Markdown
Member

LGTM.

I think we should accept options for loopback.rest() too.

Comment thread lib/middleware/rest.js

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accept AccessToken as an option to the middleware and default to getModelByType().

IMO Implementing app.getModelByType() should be trivial.

@bajtos

bajtos commented May 16, 2014

Copy link
Copy Markdown
Member Author

I'd rather avoid passing the access-token model to loopback.rest, as I am intending to call it from app.boot in the future.

How about adding an option to app.enableAuth()?

app.enableAuth({ accessTokenModel: AccessToken });
// stores the model somewhere, e.g. app._authAccessTokenModel

// app.token() or app.rest() can use app._authAccessToken as the default

Or I can implement app. getModelByType as you have mentioned. I have already done that in #167, but you had convincing reasons why app.getModelByType was not a good idea at that time.

@bajtos

bajtos commented May 19, 2014

Copy link
Copy Markdown
Member Author

Here is my proposal: merge the patch as it is. Introduce access token option when there is a user asking for it. How about that, @ritch @raymondfeng?

@slnode

slnode commented May 21, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/1262/

Make `loopback.rest` self-contained, so that authentication works
out of the box.

    var app = loopback();
    app.enableAuth();
    app.use(loopback.rest());

Note that cookie parsing middleware is not added, users have to
explicitly configure that if they want to store access tokens
in cookies.

Modify `loopback.token` to skip token lookup when the request already
contains `accessToken` property. This is in line with other
connect-based middleware like `cookieParser` or `json`.
@bajtos

bajtos commented May 21, 2014

Copy link
Copy Markdown
Member Author

Rebased on top of current master, added one more unit test.

@ritch @raymondfeng may I merge it?

@slnode

slnode commented May 21, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/1264/

@raymondfeng

Copy link
Copy Markdown
Member

LGTM

bajtos added a commit that referenced this pull request May 21, 2014
…-middleware-alt

Modify `loopback.rest` to include `loopback.token`
@bajtos
bajtos merged commit 570cc00 into master May 21, 2014
@bajtos
bajtos deleted the feature/include-token-in-rest-middleware-alt branch May 21, 2014 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants